﻿using System;
using System.Collections.Generic;
using System.Linq;
using EzDesk.Core.Model;
using EzDesk.Utility.TimerService;

namespace EzDesk.ServiceHost.TimerTask
{
    public class SendUpdateFeedsTask : TimerTaskBase
    {
        // 延迟发送Feed的时间长度，单位s
        const int SendUpdateFeedsDelay = 10;

        public override DateTime ExecuteTime
        {
            get { return DateTime.Now; }
            protected set
            {
                base.ExecuteTime = value;
            }
        }
        protected override void doExecute()
        {
            base.doExecute();

            // 寻找一定时间以前更新的所有task
            var updateTaskMethods =
                MvcApplication.HttpApplication["SendUpdatesMethods"] as List<SendUpdateFeedsMethod>;
            if (updateTaskMethods != null)
            {
                var query = updateTaskMethods.Where(p => p.UpdateOn < DateTime.Now.AddSeconds(-SendUpdateFeedsDelay));
                foreach (var updateTaskMethod in query)
                {
                    updateTaskMethods.Remove(updateTaskMethod);
                    updateTaskMethod.Execute();
                }
            }

        }
    }
}